package com.borya.filter.auth;

import java.util.Objects;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.borya.util.md5.MD5Util;

public class AuthService {

	protected Logger log = Logger.getLogger(getClass());

	protected JSONObject decryptBody(AuthRequest request) {
		JSONObject json = JSONObject.parseObject(request.getBody());

		if (!Objects.equals(AuthHelper.getDivideChar(AuthHelper.getSeparateChar(MD5Util.md5Hex(request.getBody()))),
				request.getSign())) {
			log.warn("Params error,url=" + request.getUrl() + "," + request.getBody());
			throw new IllegalStateException("md5解密失败");
		}
		return json;
	}

	// private String decrypt(byte[] key, String base64_str) {
	// try {
	// // 解密
	// Base64.Decoder decoder = Base64.getDecoder();
	// byte[] in = decoder.decode(base64_str);
	// if (in.length % 16 != 0) {
	// return "";
	// }
	// byte[] iv = new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	// 0x08,
	// 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
	// SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
	// IvParameterSpec ivSpec = new IvParameterSpec(iv);
	// Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
	// cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
	// byte[] decrypted = cipher.doFinal(in);
	// return new String(decrypted, "UTF-8").replaceAll("\r|\n", "");
	// } catch (Exception e) {
	// log.error(e.getMessage(), e);
	// return "";
	// }
	// }

}
